package list;

public class MyArrlist<T> implements MyList<T> {
    private Object[] arr = new Object[10];
    private int size = 0;

    @Override
    public boolean add(T date) {
        kuorong();
        arr[size] = date;
        size++;
        return true;
    }

    @Override
    public boolean add(int index, T date) {

        kuorong();
        if (index < 0 || index > arr.length) {
            throw new RuntimeException("数组越界");
        }
        for (int i = size - 1; i > index; i--) {
            arr[i] = arr[i - 1];
        }
        arr[index] = date;
        size++;
        return true;
    }



    @Override
    public T remove(T date) {
        int id=-1;
        for (int i=0;i<size;i++){
            if (arr[i]==date){
              id=i;
            }
        }
        if (id==-1) {
            throw new RuntimeException("数据不存在");
        }else {
            for (int i=id;i<size;i++){
                arr[i]=arr[i+1];
            }
        }

        return null;
    }

    @Override
    public T remove(int index) {
        if (index < 0 || index > arr.length) {
            throw new RuntimeException("数组越界");
        }else {
            for (int i=index;i<size;i++){
                arr[i]=arr[i+1];
            }
        }
        return null;
    }

    @Override
    public boolean set(int index, T date) {
        arr[index]=date;
        return true;
    }

    @Override
    public T getValue(int index) {
        T date= (T) arr[index];
        return date;
    }

    @Override
    public int size() {
        return size;
    }

    private void kuorong() {
        if (size >= arr.length) {
//            出现问题
            Object[] temp = new Object[size * 2];
            for (int i = 0; i < size; i++) {
                temp[i] = arr[i];
            }
            arr = temp;
        }
    }
}
